//
//  WelViewController.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/12.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelViewController: UIViewController {
    
    override func loadView() {
        view = backImage
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
     
        //        startAnimation()
    }
    
    
    ///不要再viewDidLoad方法中去做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        
        //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
        //initialSpringVelocity 加速度
        //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
        
        //外部改变约束
        self.iconImage.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        })
        
        
        //动画效果 由近及远 涉及到加速度
        UIView.animateWithDuration(1.2, delay:0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //闭包内部 强制刷新页面
            //将提前收集到的约束的变化 进行更新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    }, completion: { (_) -> Void in
                        // 老顾客登陆,发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                })
        }
    }

    //MARK:1.添加子视图
    private func setupUI() {
        // 1.添加子控件
        view.addSubview(iconImage)
        view.addSubview(welcomeLable)
        
        // 2.设置布局
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //加载图片
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //给图片处理成圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    
    }
    
    // 懒加载所有视图控件
    
    // 背景视图
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var welcomeLable: UILabel = {
        let l = UILabel()
        
        l.textColor = UIColor.lightGrayColor()
        l.font = UIFont.systemFontOfSize(19)
        l.textAlignment = NSTextAlignment.Center
        l.text = (UserAccountViewModel().userName ?? "") + "欢迎回来"
        l.alpha = 0
        l.sizeToFit()
        return l
    }()
}
